ARTICLES > PHP

PHP preserve break line on textarea when insert to database Turn Back

2016-11-29 15:40:42

 

In case user enters text and press enter for new lines into textarea of form. Break line won't insert to database.
So this way to make easy the line breaks within database.

This is line
This is line
This is line

$text = nl2br($_POST['text']);

 

// On database
This is line<br />
This is line<br />
This is line<br />

 

If you want to fetch it from database and write back into textarea.

<?php 
function br2nl($str){
 return preg_replace('#<brs*/?>#i', "", $str);
 }
?>
 
<textarea><? echo br2nl($text);?></textarea>
 
Hope you enjoy coding

 

 

Turn Back